package com.xkh.common.common.constant;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;


@ApiModel("返回值")
public class Result<T> {
    @ApiModelProperty("code 200：成功,500：系统内部错误 401：未授权")
    private Integer code;
    @ApiModelProperty("时间戳")
    private Long time;
    @ApiModelProperty("MD5")
    private String sign;
    @ApiModelProperty("数据")
    private T data;
    @ApiModelProperty("返回信息")
    private String message;
    @ApiModelProperty("其他接口调用返回数据")
    private Object other;

    public static <T> Result<T> succeed() {
        return getJSON("操作成功", HttpStatus.HTTP_OK);
    }

    public static <T> Result<T> succeed(String message) {
        return getJSON(message, HttpStatus.HTTP_OK);
    }


    public static <T> Result<T> succeedData(T data) {
        return getJSON(data,"操作成功", HttpStatus.HTTP_OK,null);
    }

    public static <T> Result<T> succeedData(T data,Object other) {
        return getJSON(data,"操作成功", HttpStatus.HTTP_OK,other);
    }

    public static <T> Result<T> otherSuccess(Object other) {
        return getJSON(null, "操作成功", HttpStatus.HTTP_OK, other);
    }

    public static <T> Result<T> otherSuccess(String message, Object other) {
        return getJSON(null, message, HttpStatus.HTTP_OK, other);
    }

    public static <T> Result<T> failure() {
        return getJSON("操作失败", HttpStatus.HTTP_INTERNAL_ERROR);
    }

    public static <T> Result<T> failure(String message) {
        return getJSON(message, HttpStatus.HTTP_INTERNAL_ERROR);
    }


    public static <T> Result<T> ResultCode(String message, Integer code) {
        return getJSON(message, code);
    }

    private static <T> Result<T> getJSON(String message, Integer code) {
        return getJSON(null, message, code, null);
    }

    /**
     * 通用
     */
    private static <T> Result<T> getJSON(T data, String message, Integer code, Object other) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setData(data);
        result.setMessage(message);
        result.setTime(System.currentTimeMillis());
        result.setOther(other);
        result.setSign(SecureUtil.md5(JSONUtil.toJsonStr(message)));
        return result;
    }


    public Object getOther() {
        return other;
    }

    public void setOther(Object other) {
        this.other = other;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
